package com.tensquare.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tensquare.entity.Result;
import com.tensquare.notice.client.ArticleClient;
import com.tensquare.notice.client.UserClient;
import com.tensquare.notice.mapper.TbNoticeFreshMapper;
import com.tensquare.notice.mapper.TbNoticeMapper;
import com.tensquare.notice.pojo.TbNotice;
import com.tensquare.notice.pojo.TbNoticeFresh;
import com.tensquare.notice.service.ITbNoticeService;
import com.tensquare.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author XXX
 * @since 2022-11-09
 */
@Service
public class TbNoticeServiceImpl extends ServiceImpl<TbNoticeMapper, TbNotice> implements ITbNoticeService {
    
    @Autowired
    private TbNoticeMapper noticeMapper;
    @Autowired
    private TbNoticeFreshMapper noticeFreshMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ArticleClient articleClient;
    @Autowired
    private UserClient userClient;

    /**
     * 完善消息内容
     * @param notice
     */
    private void getInfo(TbNotice notice){
        //查询用户昵称
        Result userResult = userClient.selectByUserId(notice.getOperatorid());
        HashMap userMap= (HashMap) userResult.getData();
        notice.setOperatorName(userMap.get("nickname").toString());
        
        //查询对象名称
        Result articleResult = articleClient.findById(notice.getTargetid());
        HashMap articleMap= (HashMap)articleResult.getData();
        notice.setTargetName(articleMap.get("title").toString());
    }
    
    
    
    

    /**
     * 删除待推送消息（新消息）
     * @param noticeFresh
     */
    @Override
    public void freshDelete(TbNoticeFresh noticeFresh) {
        noticeFreshMapper.delete(new QueryWrapper<>(noticeFresh));
    }

    /**
     * 根据用户Id来查询该用户的带推送消息（新消息）
     * @param userId
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<TbNoticeFresh> freshPage(String userId, Integer page, Integer size) {
        TbNoticeFresh tbNoticeFresh = new TbNoticeFresh();
        tbNoticeFresh.setUserid(userId);
        Page<TbNoticeFresh> pageDate = new Page<>(page, size);
        Page<TbNoticeFresh> noticeFreshPage = noticeFreshMapper.selectPage(pageDate, new QueryWrapper<>(tbNoticeFresh));
        return noticeFreshPage;
    }

    @Override
    public void saveNoticeAndNoticeFresh(TbNotice notice) {
        
        //设置初始值创建通知时间
        notice.setCreatetime(new Date());
        //使用分布式Id生成器生成Id
        String id = idWorker.nextId() + "";
        notice.setId(id);
        noticeMapper.insert(notice);

        //待推送消息入库，新消息提醒
        TbNoticeFresh noticeFresh = new TbNoticeFresh();
        noticeFresh.setUserid(notice.getReceiverid());//待通知用户的Id
        noticeFresh.setNoticeid(id);//消息Id
        noticeFreshMapper.insert(noticeFresh);
    }

    /**
     * 根据条件分页查询消息通知
     * @param notice
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<TbNotice> selectByList(TbNotice notice, Integer page, Integer size) {
       
        
        //封装分页对象
        Page<TbNotice> noticePage = new Page<>(page, size);
        Page<TbNotice> tbNoticePage = noticeMapper.selectPage(noticePage, new QueryWrapper<>(notice));
         
        //完善消息
        List<TbNotice> records = tbNoticePage.getRecords();
        for (TbNotice record : records) {
            getInfo(record);
        }
        
        return tbNoticePage;
    }

    /**
     * 根据消息Id查询消息通知
     * @param id
     * @return
     */
    @Override
    public TbNotice selectById(String id) {
        TbNotice notice = noticeMapper.selectById(id);
        //完善消息
        getInfo(notice);
        return notice; 
    }
}
